<?php
header("content-type:text/html;charset=utf-8");
session_start();
include "public/is_login.php";
include "../public/config/config.php";
include "../public/include/funcs.php";

$id = $_GET['id'] ? $_GET['id'] : $_SESSION['id'];
if(!$id) $id = $did;

if($id == 0){
	echo "<script>alert('参数错误！');</script>";
//	echo "您已经是顶级会员，没有上级。";
	header('refresh:2;url=user.php');
}else{
	$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
	$arr = select($link, "user", "id=$id");
	$name = $arr[0]['tname'] ? $arr[0]['tname'] : $arr[0]['nickname'];
	$u_img = $arr[0]['u_img'];
	
	$pid = $arr[0]['pid'];
	if($pid == 0){
		$pname = "您没有推荐人";
	}else{
		$parr = select($link, "user", "id=$pid");
		$pname = $parr[0]['tname'] ? $parr[0]['tname'] : $parr[0]['nickname'];
	}
	
	$pid2 = $arr[0]['pid2'];
	if($pid2 == 0){
		$pname2 = "您没有接点人";
	}else{
		$parr2 = select($link, "user", "id=$pid2");
		$pname2 = $parr2[0]['tname'] ? $parr2[0]['tname'] : $parr2[0]['nickname'];
	}
	
	$pid_save = $arr[0]['pid_save'];
	if($pid_save){
		$disabled = "disabled";
		if(!$_POST) echo "<script>alert('您已经修改过接点人ID，不能再次修改。');</script>";
	}else{
		if($pid == 0){
			$disabled = "disabled";
		}else{
			$disabled = "";
		}
		if(!$_POST) echo "<script>alert('注意：接点人ID只能修改一次，请谨慎修改！');</script>";
	}
	
}


if($_POST){
	if($login){
		$save_pid2 = $_POST['pid2'];
		$arr_pid2 = select($link, "user", "id=$save_pid2");
		if(!$arr_pid2){
			echo "<script>";
			echo "alert('该接点人ID不存在，修改失败！');";
			echo "window.location.href='user.php';";
			echo "</script>";
		}else{
			$save_arr['pid2'] = $_POST['pid2'];
			$save_arr['pid_save'] = 1;
			
			$re = save($link, "user", $save_arr, "id=$id");
			if($re){
				echo "<script>";
				echo "alert('修改成功！');";
				echo "window.location.href='user.php';";
				echo "</script>";
			}else{
				echo "<script>";
				echo "alert('修改失败！');";
	//			echo "window.location.href='user.php';";
				echo "</script>";
			}
		}
	}else{
		echo "<script>";
		echo "alert('非法请求！');";
		echo "window.location.href='login.php';";
		echo "</script>";
	}

}
mysqli_close($link);

//echo "<pre>";
//print_r($arr);
//print_r($parr);
//echo "</pre>";
?>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
	<title>分享商城 -- 设置接点人</title>
	
	<!-- Bootstrap -->
	<link href="/public/css/bootstrap.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="public/main.css"/>
	
	<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
	<!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
	<!--[if lt IE 9]>
	<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
	<![endif]-->
</head>
<body class="bg_lit">

<!--<div class="container">
	<img class="list-logo" src="imgs/list/logo.png"/>
</div>-->

<div class="head_top">
	<div class="u_img">
		<img class="img-rounded" src="<?php echo $u_img;?>"/>
	</div>
	<div class="u_name">
		<p class="u_title"><?php echo $name;?></p>
		<p class="u_id">ID号：<?php echo $id;?></p>
	</div>
	<div class="clear"></div>
</div>

<div class="container">
	<p id="abc" class="wans_info">修改我的接点人：</p>
	<div class="info_form">
		<form action="#" method="post" class="form-horizontal" name="frm" onsubmit="return check()">
			
			<div class="form-group mar_top">
				<label for="pname" class="control-label col-xs-4">推荐人：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" disabled="disabled" name="pname" id="" value="<?php echo $pname;?>" />
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="pid" class="control-label col-xs-4">推荐人ID：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" disabled="disabled" name="pid" id="" value="<?php echo $pid;?>" />
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="pname2" class="control-label col-xs-4">接点人：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" disabled="disabled" name="pname2" id="pname2" value="<?php echo $pname2;?>" />
				</div>
			</div>
			<div class="form-group mar_top">
				<label for="pid2" class="control-label col-xs-4">接点人ID：</label>
				<div class="col-xs-8">
					<input type="text" class="form-control" <?php echo $disabled;?> name="pid2" id="pid2" value="<?php echo $pid2;?>" />
				</div>
			</div>
			<p id="is_pname"></p>
			
			<input type="submit" <?php echo $disabled;?> class="btn btn-success btn-block btn-lg mar_top" value="提交"/>
		</form>
	</div>
	
	
</div>


<?php include "template/footer.php";?>

<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script type="text/javascript" src="/public/js/jquery-3.3.1.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
<script type="text/javascript">
function check(){
	if(confirm("该信息只能修改一次，确实要修改吗？")) return true;else return false;
}

$("#pid2").keyup(function(){
	var pid_int=$("#pid2").val();
	var ajax = new XMLHttpRequest();
	ajax.open('get','ajax_getName.php?id='+pid_int);
	ajax.send();
	ajax.onreadystatechange = function () {
		if (ajax.readyState==4 &&ajax.status==200) {
			var date_name = JSON.parse(ajax.responseText);
			$("#is_pname").html('ID为' + pid_int + '的会员是： ' + date_name);
		}
	}
});

</script>
</body>
</html>